package org.dromara.hm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.hm.domain.bo.HmCustomerBatchTransferBo;
import org.dromara.hm.domain.bo.HmCustomerBo;
import org.dromara.hm.domain.vo.HmCustomerExcelVo;
import org.dromara.hm.domain.vo.HmCustomerVo;
import org.dromara.hm.domain.vo.HmPurchaseVo;
import org.dromara.hm.domain.vo.HmStaffTransferVo;
import org.dromara.hm.domain.vo.HmTodoItemsVo;
import org.dromara.hm.listener.HmCustomerImportListener;
import org.dromara.hm.service.IHmCustomerService;
import org.dromara.system.domain.SysUser;
import org.dromara.system.domain.vo.SysUserVo;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户信息
 *
 * @author zy Hu
 * @date 2024-12-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hm/customer")
public class HmCustomerController extends BaseController
{

    private final IHmCustomerService hmCustomerService;

    /**
     * 查询客户信息列表
     */
    @SaCheckPermission("hm:customer:list")
    @GetMapping("/list")
    public TableDataInfo<HmCustomerVo> list(HmCustomerBo bo, PageQuery pageQuery)
    {
        return hmCustomerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出客户信息列表
     */
    @SaCheckPermission("hm:customer:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmCustomerBo bo, HttpServletResponse response)
    {
        List<HmCustomerVo> list = hmCustomerService.queryList(bo);
        ExcelUtil.exportExcel(list, "客户信息", HmCustomerVo.class, response);
    }

    /**
     * 获取客户信息详细信息
     *
     * @param customerId 主键
     */
    @SaCheckPermission("hm:customer:query")
    @GetMapping("/{customerId}")
    public R<HmCustomerVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long customerId)
    {
        return R.ok(hmCustomerService.queryById(customerId));
    }

    /**
     * 新增客户信息
     */
    @SaCheckPermission("hm:customer:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmCustomerBo bo)
    {
        return toAjax(hmCustomerService.insertByBo(bo));
    }

    /**
     * 修改客户信息
     */
    @SaCheckPermission("hm:customer:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmCustomerBo bo)
    {
        return toAjax(hmCustomerService.updateByBo(bo));
    }

    /**
     * 修改家庭信息
     */
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateFamily")
    public R<Void> updateFamily( @RequestBody HmCustomerBo bo)
    {
        return toAjax(hmCustomerService.updateFamily(bo));
    }

    /**
     * 删除客户信息
     *
     * @param customerIds 主键串
     */
    @SaCheckPermission("hm:customer:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] customerIds)
    {
        return toAjax(hmCustomerService.deleteWithValidByIds(List.of(customerIds), true));
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil.exportExcel(new ArrayList<>(), "客户数据", HmCustomerVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "客户管理", businessType = BusinessType.IMPORT)
    @SaCheckPermission("hm:customer:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelResult<HmCustomerExcelVo> result = ExcelUtil.importExcel(file.getInputStream(), HmCustomerExcelVo.class, new HmCustomerImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 根据服务人员ID查询名下所有的客户信息
     *
     * @param staffId
     *
     * @return
     */
    @GetMapping("/list/{staffId}")
    public R<List<HmCustomerVo>> listByStaffId(@PathVariable Long staffId)
    {
        List<HmCustomerVo> hmCustomerVos = hmCustomerService.queryListByStaffId(staffId);
        return R.ok(hmCustomerVos);
    }

    /**
     * 根据服务人员ID查询名下所有的客户信息
     *
     * @param staffId
     *
     * @return
     */
    @PostMapping("/list/staffId")
    public R<List<HmCustomerVo>> listByStaffIds(@RequestBody Long[] staffId)
    {
        List<HmCustomerVo> hmCustomerVos = hmCustomerService.queryListByStaffIds(staffId);
        return R.ok(hmCustomerVos);
    }

    /**
     * 查询未分配家庭组客户列表
     */
    @GetMapping("/family/unallocatedList")
    public TableDataInfo<HmCustomerVo> unallocatedList(HmCustomerBo customer, PageQuery pageQuery)
    {
        return hmCustomerService.selectUnallocatedList(customer, pageQuery);
    }

    /**
     * 查询加入家庭组客户列表
     */
    @GetMapping("/family/locatedList")
    public TableDataInfo<HmCustomerVo> familyAllocatedList(HmCustomerBo customer, PageQuery pageQuery)
    {
        return hmCustomerService.selectLocatedList(customer, pageQuery);
    }

    /**
     * 根据客户ID查询所有的购买记录与回款记录
     */
    @GetMapping("/{customerId}/purchaseRecord")
    public R<HmPurchaseVo> purchaseRecordList(@PathVariable Long customerId, PageQuery pageQuery)
    {
        return R.ok(hmCustomerService.queryPurchaseRecordList(customerId, pageQuery));
    }

    /**
     * 批量转移客户
     */
    @SaCheckPermission("hm:customer:transfer") // Define appropriate permission
    @Log(title = "客户转移", businessType = BusinessType.UPDATE)
    @PostMapping("/transfer/batch-transfer")
    public R<Void> transferCustomers(@Validated @RequestBody HmCustomerBatchTransferBo bo) {
        return toAjax(hmCustomerService.batchTransferCustomers(bo));
    }

    /**
     * 加载离职员工以及员工名下客户信息
     */
    @GetMapping("/transfer/resigned-staff-customers")
    public R<HmStaffTransferVo> resignedStaffCustomersList(Long storeId)
    {
        return R.ok(hmCustomerService.resignedStaffCustomersList(storeId));
    }



}
